package com.vitabuybuy.tv.ui.mine.viewmodel

import androidx.lifecycle.MutableLiveData
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.toast.XToast
import com.vitabuybuy.tv.dto.LoginInfoDto
import com.vitabuybuy.tv.network.AddressRepository
import com.vitabuybuy.tv.network.AuthRepository
import com.vitabuybuy.tv.network.MineRepository

class PersonalInfoViewModel : BaseViewModel() {

    val userInfoDto = MutableLiveData<LoginInfoDto>()

    fun getUserInfo() {
        rxHttpRequest {
            onRequest = {
                userInfoDto.value = MineRepository.getInfoByToken().await()
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    fun uploadImage(path: String) {
        rxHttpRequest {
            onRequest = {
                val it = AuthRepository.upLoadImage(path).await()
                defUI.msgEvent.postValue(MessageModel(1, obj = it))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun resetUserInfo(key: String, value: Any?) {
        rxHttpRequest {
            onRequest = {
                val it = MineRepository.resetUserInfo(key, value).await()
                defUI.msgEvent.postValue(MessageModel(2, msg = key, obj = value))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun getRegionList(regionId: Int?) {
        rxHttpRequest {
            onRequest = {
                val it = AddressRepository.getRegionList(regionId).await()
                defUI.msgEvent.postValue(MessageModel(if (regionId == null) 4 else 3, obj = it))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }
}
